package com.alibaba.nacos.example.spring.cloud.test;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

/**
 * 数据回显公共类
 */
//空不转换
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@NoArgsConstructor
@Getter
@Setter
public class ServiceResponse<T> implements Serializable {

    /**
     * 需要的三个属性
     */
    private int code; // 状态码
    private String msg; // 返回的消息
    private T data; //返回的实体 数据体

    /**
     * 自定义构造方法
     */
    private ServiceResponse(int code){
        this.code = code;
    }
    private ServiceResponse(int code,String msg){
        this.code = code;
        this.msg = msg;
    }
    private ServiceResponse(int code,String msg,T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    private ServiceResponse(int code,T data){
        this.code = code;
        this.data = data;
    }

    /**
     * 对外的方法
     * 不显示在json中
     */
    @JsonIgnore
    public Boolean isSuccess(){
        return this.code==ResponseCode.SUCCESS.getCode();
    }

    public Boolean isFaild(){return this.code==ResponseCode.ERROR.getCode();}

    /**
     * 开放属性
     */
    public int getStatus(){
        return this.code;
    }
    public String getMsg(){
        return this.msg;
    }
    public T getData(){
        return this.data;
    }
    /**
     * 静态方法
     */
    public static <T> ServiceResponse<T> creatBySuccess(T data){
        return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(),data);
    }
    public static  <T> ServiceResponse<T> creatBySuccessMessage(String msg){
        return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
    }
    public static <T> ServiceResponse<T> creatBySuccess(){
        return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode());
    }
    public static <T> ServiceResponse<T> creatBySuccess(String msg , T data){
        return new ServiceResponse<T>(ResponseCode.SUCCESS.getCode(),msg,data);
    }
    /**
     * 失败方法
     */
    public static <T> ServiceResponse<T> creatByError(){
        return new ServiceResponse<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());
    }
    public static <T> ServiceResponse<T> creatByErrorMessage(String message){
        return new ServiceResponse<T>(ResponseCode.ERROR.getCode(),message);
    }

    /**
     * 带变量的信息码
     * @param <T>
     * @return
     */
    public static <T> ServiceResponse<T> creatByError(int code , String msg){
        return new ServiceResponse<T>(code,msg);
    }


}
